package com.cango.qutz.web.auth.position.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cango.qutz.web.auth.position.dao.IPositionDao;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.qutz.web.auth.position.dao.IPositionDao;
import com.cango.qutz.web.auth.position.service.IPositionService;
import com.cango.qutz.web.entity.PositionEntity;
import com.cango.qutz.web.entity.PositionRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cango.qutz.web.auth.position.service.IPositionService;
import com.cango.qutz.web.entity.PositionEntity;
import com.cango.qutz.web.entity.PositionRoleEntity;
import com.cango.framework.base.pagination.PaginationResult;

@Service("positionService")
public class PositionServiceImpl implements IPositionService {
    /** 岗位sql的命名空间 */
    private static final String SQL_NAMESPACE = "position.";

    /** 新增岗位的sql id */
    private static final String SQL_INSERT = "insertPosition";

    /** 更新岗位的sql id */
    private static final String SQL_UPDATE = "updatePosition";

    /** 删除岗位的sql id */
    private static final String SQL_DELETE = "deletePosition";

    /** 根据主键查询岗位的sql id */
    private static final String SQL_QUERY_BY_ID = "queryPositionById";

    /** 查询岗位的sql id */
    private static final String SQL_QUERY_FOR_LIST = "queryPositions";

    /** 分页查询岗位的sql id */
    private static final String SQL_QUERY_BY_PAGE = "queryPositionsByPage";

    /** 根据机构查询岗位的sql id */
    private static final String SQL_QUERY_BY_ORG = "queryPositionsByOrg";

    /** 根据机构查询岗位的sql id */
    private static final String SQL_QUERY_COUNT_BY_CODE = "queryCountByCode";

    /** 根据岗位代码查询岗位的sql id */
    private static final String SQL_QUERY_POS_BY_CODE = "queryPosByCode";

    /** 根据岗位角色关系的sql id */
    private static final String SQL_INSERT_POSITIONROLE = "insertPositionRole";

    /** 删除岗位角色关系的sql id */
    private static final String SQL_DELETE_POSITIONROLE = "deletePositionRole";

    /** 删除岗位角色关系的sql id */
    private static final String SQL_QUERY_USER_COUNT_BY_POS = "queryUserCountByPos";

    @Autowired
    private IPositionDao positionDao;

    @Override
    public int insert(PositionEntity position) {
        position.setPositionPath(position.getParentCode() + ">>" + position.getPositionCode());
        position.setIsDel(0);
        position.setStatus(1);
        int id = positionDao.insert(SQL_NAMESPACE + SQL_INSERT, position);
        return id;
    }

    @Override
    public int update(PositionEntity position) {
        PositionEntity posEdit = this.queryObjectById(position.getPositionId());
        posEdit.setParentCode(position.getParentCode());
        posEdit.setPositionName(position.getPositionName());
        posEdit.setPositionPath(position.getParentCode() + ">>" + position.getPositionCode());
        posEdit.setRemark(position.getRemark());
        posEdit.setUpdateUser(position.getUpdateUser());
        posEdit.setUpdateTime(new Date());
        return positionDao.update(SQL_NAMESPACE + SQL_UPDATE, posEdit);
    }

    @Override
    public int delete(PositionEntity position) {
        return positionDao.delete(SQL_NAMESPACE + SQL_DELETE, position);
    }

    @Override
    public PositionEntity queryObjectById(Integer positionId) {
        return positionDao.queryObjectById(SQL_NAMESPACE + SQL_QUERY_BY_ID, positionId);
    }

    @Override
    public List<PositionEntity> queryList(Object param) {
        return positionDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST, param);
    }

    @Override
    public PaginationResult<PositionEntity> queryListByPage(PositionEntity param, PageInfo pageinfo) {
        return positionDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_BY_PAGE, param, pageinfo);
    }

    @Override
    public List<PositionEntity> queryPositionListByOrg(PositionEntity param) {
        return this.positionDao.queryList(SQL_NAMESPACE + SQL_QUERY_BY_ORG, param);
    }

    @Override
    public boolean validCode(String positionCode) {
        int count = this.positionDao.queryCountByCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_CODE, positionCode);
        return count > 0 ? false : true;
    }

    @Override
    public PositionEntity queryPosByCode(String positionCode) {
        return this.positionDao.queryForObject(SQL_NAMESPACE + SQL_QUERY_POS_BY_CODE, positionCode);
    }

    @Override
    public void savePositionRole(PositionRoleEntity posRole) {
        String[] roleArr = posRole.getRoleCode().split(",");
        List<PositionRoleEntity> posRoleList = new ArrayList<PositionRoleEntity>();
        for (int i = 0; i < roleArr.length; i++) {
            PositionRoleEntity postionRole = new PositionRoleEntity();
            postionRole.setPositionCode(posRole.getPositionCode());
            postionRole.setCreateUser(posRole.getCreateUser());
            postionRole.setCreateTime(new Date());
            postionRole.setRoleCode(roleArr[i]);
            posRoleList.add(postionRole);
        }
        this.positionDao.insert(SQL_NAMESPACE + SQL_INSERT_POSITIONROLE, posRoleList);
    }

    @Override
    public void deletePositionRole(PositionRoleEntity posRole) {
        this.positionDao.delete(SQL_NAMESPACE + SQL_DELETE_POSITIONROLE, posRole);
    }

    @Override
    public int queryUserCountByPos(Integer positionId) {
        int count = this.positionDao.queryUserCountByPos(SQL_NAMESPACE + SQL_QUERY_USER_COUNT_BY_POS, positionId);
        return count;
    }

}
